#include "gtkCallBacks.h"

void timeChange(GtkButton* btn, gpointer data)
{
    if (isCounting)
    {
        return;
    }
    if ((unsigned int)(unsigned long long int)data == 0) // less time
    {
        if (isResting)
        {
            if (minuteRes > 5)
            {
                minuteRes--;
            }
        }
        else
        {
            if (minuteWork > 10)
            {
                minuteWork--;
            }
        }
    }
    else // more time
    {
        if (isResting)
        {
            if (minuteRes < 40)
            {
                minuteRes++;
            }
        }
        else
        {
            if (minuteWork < 120)
            {
                minuteWork++;
            }
        }
    }
    countMinute = isResting ? minuteRes : minuteWork;
    resetCounting(nullptr, nullptr);
}

void resetCounting(GtkButton* btn, gpointer data)
{
    isCounting = false;
    countSecond = 0;
    countMinute = isResting ? minuteRes : minuteWork;
    gtk_label_set_label(GTK_LABEL(labelMinute), countMinute ? to_string(countMinute).c_str() : "00");
    gtk_label_set_label(GTK_LABEL(labelSecond), countSecond ? to_string(countSecond).c_str() : "00");
    gtk_label_set_label(GTK_LABEL(labelNote), isResting ? "休息一会儿吧" : "稍微学习一会儿，然后放松放松吧");
}

void changeStatus(GtkButton* btn, gpointer data)
{
    if (isCounting)
    {
        return;
    }
    isResting = !isResting;
    resetCounting(nullptr, nullptr);
}

void startCounting(GtkButton* btn, gpointer data)
{
    isCounting = true;
}


void DeleteEvent(GObject* window, gpointer data)
{
    killTheProg = true;
    gtk_main_quit();
}


gboolean countDown(gpointer label)
{
    if (isCounting)
    {
        countSecond -= 1;
        while (countSecond < 0)
        {
            countSecond += 60;
            countMinute -= 1;
        }
        if (countMinute < 0)
        {
            resetCounting(nullptr, nullptr);
            changeStatus(nullptr, nullptr);
            resetCounting(nullptr, nullptr);
        }
    }
    else
    {
        countMinute = isResting ? minuteRes : minuteWork;
    }
    gtk_label_set_label(GTK_LABEL(labelMinute), countMinute ? to_string(countMinute).c_str() : "00");
    gtk_label_set_label(GTK_LABEL(labelSecond), countSecond ? to_string(countSecond).c_str() : "00");
    gtk_label_set_label(GTK_LABEL(labelNote), isResting ? "休息一会儿吧" : "稍微学习一会儿，然后放松放松吧");
    return true;
}
